//
//  DateUtils.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/11/25.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "DateUtils.h"

@implementation DateUtils


+ (NSDate *)firstDayOfCurrentMonth
{
    NSDate *startDate = nil;
    BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startDate interval:NULL forDate:self];
    NSAssert1(ok, @"Failed to calculate the first day of the month based on %@", self);
    return startDate;
}
+ (int)dayOfMonth:(NSString *)date;
{
    NSDateFormatter *sf = [[NSDateFormatter alloc]init];
    [sf setDateFormat:@"yyyy-MM"];
    NSDate *today = [sf dateFromString:date]; //Get a date object for today's date

    

    NSCalendar *c = [NSCalendar currentCalendar];
    NSRange days = [c rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:today];
    
    
    return days.length;
}
+ (NSString *)getMonth
{
  
    NSDate *today = [NSDate date]; //Get a date object for today's date
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSCalendarUnitMonth | NSCalendarUnitDay;
    
    comps = [calendar components:unitFlags fromDate:today];
    
    int month = [comps month];
    NSString *str = @"";
     if(month >= 10){
         str = [NSString stringWithFormat:@"%i",month];
     } else{
        str = [NSString stringWithFormat:@"0%i",month];
     }
    return str;
}
+ (int)getYear
{
    
    NSDate *today = [NSDate date]; //Get a date object for today's date
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSCalendarUnitMonth | NSCalendarUnitDay|NSWeekdayCalendarUnit;
    
    comps = [calendar components:unitFlags fromDate:today];
    
    int year = [comps year];
    return year;
}

+ (int)getWeek:(NSString *)date
{
    NSDateFormatter *sf = [[NSDateFormatter alloc]init];
    [sf setDateFormat:@"yyyy-MM-dd"];
    NSDate *today = [sf dateFromString:date]; //Get a date object for today's date
    
    NSLog(@"%@",[sf stringFromDate:today]);
    NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
   
    
    comps = [calendar components:NSWeekdayCalendarUnit fromDate:today];
    
    int week = [comps weekday];
    return week;
}
+ (NSArray *)getNextMonth:(int)year month:(int)month{
    if (month + 1 == 13) {
        return @[[NSString stringWithFormat:@"%i",year + 1],@"1"];
    }else{
        return @[[NSString stringWithFormat:@"%i",year],[NSString stringWithFormat:@"%i",month + 1]];
    }
}
+ (NSArray *)getLastMonth:(int)year month:(int)month{
    if (month - 1 == 0) {
       return @[[NSString stringWithFormat:@"%i",year - 1],@"12"];
    }else{
       return @[[NSString stringWithFormat:@"%i",year],[NSString stringWithFormat:@"%i",month - 1]];
    }
}
+ (NSString *)getDate{
    NSDateFormatter *sf = [[NSDateFormatter alloc]init];
    [sf setDateFormat:@"yyyy-MM-dd"];
    
    return [sf stringFromDate:[NSDate date]];
}

+ (NSString *)getFirstDay:(int)year month:(int)month{
    if (month >= 10) {
        return [NSString stringWithFormat:@"%i-%i-01",year,month];
    }else{
        return [NSString stringWithFormat:@"%i-0%i-01",year,month];
    }
}
+ (NSString *)getEndDay:(int)year month:(int)month{
    int day = [self dayOfMonth:[NSString stringWithFormat:@"%i-%i",year,month]];

    if (month >= 10) {
        return [NSString stringWithFormat:@"%i-%i-%i",year,month,day];
    }else{
        return [NSString stringWithFormat:@"%i-0%i-%i",year,month,day];
    }
}
@end
